<?php
/**
 * Model.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/11
 * Time: 22:12
 */
namespace app\console\command\auto\tpl;

use app\api\util\Hump;

class Model
{
    static public function template($method,$table)
    {
        $className = Hump::camel($table).'Model';
        $date = date('Y/m/d',time());
        $time = date('H:i',time());
        $tpl = <<<EOF
<?php
/**
 * $className.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: $date
 * Time: $time
 */
namespace app\\api\\model;

use app\\common\\Singleton;
use think\\Db;

class $className
{
    use Singleton;

    /**
     * 添加|修改
     * @param \$flag
     * @param \$data
     * @param array \$extend
     * @return int|string
     */
    public function save(\$flag,\$data,\$extend=[])
    {
        if(empty(\$flag))
        {
            // 添加
            \$data['create_ts'] = time();
            return Db::$method('$table')
                ->insertGetId(\$data);
        }
        else
        {
            // 编辑
            \$data['update_ts'] = time();
            return Db::$method('$table')
                ->where('id',\$flag)
                ->update(\$data);
        }
    }

    /**
     * 获取详情
     * @param \$where
     * @param array \$extend
     * @return array|false|null|\PDOStatement|string|\\think\Model
     */
    public function get(\$where,\$extend=[])
    {
        \$handler = Db::$method('$table');
        if(!empty(\$extend['field']))
        {
            \$handler->field(\$extend['field']);
        }
        if(!empty(\$extend['order']))
        {
            \$handler->order(\$extend['order']);
        }
        if(!empty(\$extend['group']))
        {
            \$handler->group(\$extend['group']);
        }
        return \$handler
            ->where(\$where)
            ->find();
    }

    /**
     * 删除
     * @param \$flag
     * @param array \$extend
     * @return int
     */
    public function delete(\$flag,\$extend=[])
    {
        return Db::$method('$table')
            ->where('id',\$flag)
            ->delete();
    }

    /**
     * 获取列表
     * @param \$where
     * @param array \$extend
     * @return \\think\Paginator
     */
    public function getList(\$where,\$extend=[])
    {
        \$handler = Db::$method('$table');
        if(!empty(\$extend['field']))
        {
            \$handler->field(\$extend['field']);
        }
        if(!empty(\$extend['order']))
        {
            \$handler->order(\$extend['order']);
        }
        if(!empty(\$extend['group']))
        {
            \$handler->group(\$extend['group']);
        }
        return \$handler
            ->where(\$where)
            ->paginate();
    }
    
    /**
     * @param \$where
     * @param array \$extend
     * @return array|false|\PDOStatement|string|\\think\Collection
     */
    public function getAll(\$where,\$extend=[])
    {
        \$handler = Db::$method('$table');
        if(!empty(\$extend['field']))
        {
            \$handler->field(\$extend['field']);
        }
        if(!empty(\$extend['order']))
        {
            \$handler->order(\$extend['order']);
        }
        if(!empty(\$extend['group']))
        {
            \$handler->group(\$extend['group']);
        }
        return \$handler
            ->where(\$where)
            ->limit(1000)
            ->select();
    }
    
    /**
     * @param \$where
     * @param \$data
     * @return int|string
     */
    public function update(\$where,\$data=[])
    {
        if(empty(\$where))
        {
            return false;
        }
        \$data['update_ts'] = time();
        return Db::$method('$table')
            ->where(\$where)
            ->update(\$data);
    }
}
EOF;
        $filename = APP_PATH.'api/model/'.$className.'.php';
        if(!is_file($filename))
        {
            @file_put_contents($filename,$tpl);
        }
        return $tpl;
    }
}